package com.shexianyu.simple;

import java.util.HashMap;

/**
 * 383. 赎金信
 */
public class CanConstruct {
    public boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character, Integer> map = new HashMap<>();
        //用map集合记录重复的字符
        for (int i = 0; i < magazine.length(); i++) {
            map.put(magazine.charAt(i), map.getOrDefault(magazine.charAt(i), 0) + 1);
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            if (map.containsKey(ransomNote.charAt(i))) {
                //判断map集合中是否包含字符，若包含，将值-1
                map.put(ransomNote.charAt(i), map.get(ransomNote.charAt(i)) - 1);
            } else {
                return false;
            }
            //判断集合中字符次数是否小于0
            if (map.get(ransomNote.charAt(i)) < 0) {
                return false;
            }
        }
        return true;
    }
}
